package com.jyx.healthsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyx.Data_unification.Unification;
import com.jyx.healthsys.entity.HealthGoals;
import com.jyx.healthsys.entity.UserGoalsMap;
import com.jyx.healthsys.service.IHealthGoalsService;
import com.jyx.healthsys.service.IUserGoalsMapService;
import com.jyx.healthsys.service.IUserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
class GoalsPo {
    private HealthGoals healthGoals;
    private Long pageNo;
    private Long pageSize;
}
@RestController
@RequestMapping("/goals")
public class GoalsController {
    @Autowired
    private IUserGoalsMapService userGoalsMapService;
    @Autowired
    private IHealthGoalsService iHealthGoalsService;
    @Autowired
    private IUserService userService;

    @PostMapping("/add")
    Unification addGoals(@RequestBody HealthGoals healthGoals) {
        Map<String, Object> userId = userService.getUserId();
        int id = Integer.parseInt(userId.get("id").toString());
        boolean b = iHealthGoalsService.saveOrUpdate(healthGoals);
        if (b){
            UserGoalsMap map = new UserGoalsMap();
            map.setUserId(id);
            map.setGoalsId(healthGoals.getId());
            boolean saved = userGoalsMapService.save(map);
            if (saved){
                return Unification.success();
            }else return Unification.fail();
        }
        return Unification.fail();
    }
    @PostMapping("/getGoals")
    Unification getGoals(@RequestBody GoalsPo goalsPo) {
        Map<String, Object> userId = userService.getUserId();
        int id = Integer.parseInt(userId.get("id").toString());
        LambdaQueryWrapper<UserGoalsMap> mapWrapper = new LambdaQueryWrapper<>();
        mapWrapper.eq(UserGoalsMap::getUserId, id);
        List<UserGoalsMap> list = userGoalsMapService.list(mapWrapper);
        LambdaQueryWrapper<HealthGoals> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(HealthGoals::getId,list.stream().map(UserGoalsMap::getGoalsId).collect(Collectors.toList()));
        wrapper.like(!goalsPo.getHealthGoals().getSpecificGoal().isEmpty(), HealthGoals::getSpecificGoal, goalsPo.getHealthGoals().getSpecificGoal());
        wrapper.like(!goalsPo.getHealthGoals().getGoalCategory().isEmpty(), HealthGoals::getGoalCategory, goalsPo.getHealthGoals().getGoalCategory());

        wrapper.orderByDesc(HealthGoals::getStartDate);
        Page<HealthGoals> page = new Page<>(goalsPo.getPageNo(), goalsPo.getPageSize());
        iHealthGoalsService.page(page,wrapper);
        // 将查询结果封装到Map中返回
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Unification.success(data);
    }
    @GetMapping("/deleteGoals")
        Unification deleteGoals(@RequestParam(name = "id") Integer goalsId) {
        boolean b = iHealthGoalsService.removeById(goalsId);
        if (b){
            return Unification.success();
        }
        return Unification.fail();
    }
    @PostMapping("/getGoalsNow")
    Unification getGoalsNow(@RequestBody GoalsPo goalsPo) {
        String now= LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Map<String, Object> userId = userService.getUserId();
        int id = Integer.parseInt(userId.get("id").toString());
        LambdaQueryWrapper<UserGoalsMap> mapWrapper = new LambdaQueryWrapper<>();
        mapWrapper.eq(UserGoalsMap::getUserId, id);
        List<UserGoalsMap> list = userGoalsMapService.list(mapWrapper);
        LambdaQueryWrapper<HealthGoals> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(HealthGoals::getId,list.stream().map(UserGoalsMap::getGoalsId).collect(Collectors.toList()));
        wrapper.le(HealthGoals::getStartDate,now);
        wrapper.ge(HealthGoals::getEndDate,now);
        wrapper.orderByDesc(HealthGoals::getStartDate);
        Page<HealthGoals> page = new Page<>(goalsPo.getPageNo(), goalsPo.getPageSize());
        iHealthGoalsService.page(page,wrapper);
        // 将查询结果封装到Map中返回
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Unification.success(data);
    }
}
